package cs2340.quarterblack.wheresmystuff;

import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import cs2340.quarterblack.wheresmystuff.Item.Itemtype;

public class Database extends Storage{
	public enum dbAction{CHECK_LOGIN, ADD_ACCOUNT, LOCK_ACCOUNT, UNLOCK_ACCOUNT, ADD_ITEM,
						  SET_PRIVILEDGES,DELETE_ITEM, DELETE_ACCOUNT, GET_USERS_ITEMS, 
						  GET_ACCOUNTS, CHECK_EXISTS, GET_ALL_ITEMS};
	
	@Override
	public loginResponse checkLogin(Account tempUser) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("userID", tempUser.getUserId()));
		params.add(new BasicNameValuePair("password", tempUser.getPw()));
		DBDataCapsule data = new DBDataCapsule(dbAction.CHECK_LOGIN, params);
		new DatabaseTask().execute(data);
		
		return null;
	}

	@Override
	public boolean addAccount(Account newUser) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("userID", newUser.getUserId()));
		params.add(new BasicNameValuePair("password", newUser.getPw()));
		params.add(new BasicNameValuePair("email", newUser.getEmail()));
		params.add(new BasicNameValuePair("status", newUser.getStatus()));
		params.add(new BasicNameValuePair("admin", newUser.getAdmin()));
		DBDataCapsule data = new DBDataCapsule(dbAction.ADD_ACCOUNT, params);
		new DatabaseTask().execute(data);
		
		return false;
	}

	@Override
	public boolean lockAccount(Account currentUser) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("userID", currentUser.getUserId()));
		params.add(new BasicNameValuePair("status", "locked"));
		DBDataCapsule data = new DBDataCapsule(dbAction.LOCK_ACCOUNT, params);
		new DatabaseTask().execute(data);
		
		return false;
	}

	@Override
	public boolean unlockAccount(Account currentUser) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("userID", currentUser.getUserId()));
		params.add(new BasicNameValuePair("password", "unlocked"));
		DBDataCapsule data = new DBDataCapsule(dbAction.UNLOCK_ACCOUNT, params);
		new DatabaseTask().execute(data);
		
		return false;
	}

	@Override
	public boolean addItem(Item foundItem) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("userID", foundItem.getUserId()));
		params.add(new BasicNameValuePair("name", foundItem.getName()));
		params.add(new BasicNameValuePair("description", foundItem.getDescription()));
		params.add(new BasicNameValuePair("location", foundItem.getLocation()));
		params.add(new BasicNameValuePair("date", foundItem.getDate().toString()));
		params.add(new BasicNameValuePair("category", foundItem.getStringCategory()));
		params.add(new BasicNameValuePair("type", foundItem.getType()));
		DBDataCapsule data = new DBDataCapsule(dbAction.ADD_ITEM, params);
		new DatabaseTask().execute(data);
		
		return false;
	}

	@Override
	public boolean setPrivelages(String id, boolean admin) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("userID", id));
		params.add(new BasicNameValuePair("admin", (admin ? "0" : "1")));
		DBDataCapsule data = new DBDataCapsule(dbAction.SET_PRIVILEDGES, params);
		new DatabaseTask().execute(data);
		
		return false;
	}

	@Override
	public boolean deleteAccount(String id) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("userID", id));
		DBDataCapsule data = new DBDataCapsule(dbAction.DELETE_ACCOUNT, params);
		new DatabaseTask().execute(data);
		
		return false;
	}

	@Override
	public List<Item> getUsersItems(Account currentUser, Itemtype type) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("user", currentUser.getUserId()));
		params.add(new BasicNameValuePair("type", type.toString().toLowerCase()));
		DBDataCapsule data = new DBDataCapsule(dbAction.GET_USERS_ITEMS, params);
		new DatabaseTask().execute(data);
		
		return null;
	}

	@Override
	public Account getAccount(Account acct) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("userID", acct.getUserId()));
		DBDataCapsule data = new DBDataCapsule(dbAction.GET_ACCOUNTS, params);
		new DatabaseTask().execute(data);
		
		return null;
	}

	@Override
	public boolean checkExists(String id) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("user", id));
		DBDataCapsule data = new DBDataCapsule(dbAction.CHECK_EXISTS, params);
		new DatabaseTask().execute(data);
		
		return false;
	}

	@Override
	public List<Item> getAllItems() {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		DBDataCapsule data = new DBDataCapsule(dbAction.GET_ALL_ITEMS, params);
		new DatabaseTask().execute(data);
		
		return null;
	}
	
	public class DBDataCapsule {
		private dbAction action;
		private ArrayList<NameValuePair> parameters;
		
		public DBDataCapsule(dbAction act, List<NameValuePair> params) {
			
		}

		public dbAction getAction() {
			return action;
		}

		public ArrayList<NameValuePair> getParameters() {
			return parameters;
		}
		
	}
}
